<?php

namespace App\Models\Wechat;

use Illuminate\Database\Eloquent\Model;

use Cache;

class Reply extends Model
{
	//保存在公共配置中静态名
	var $staticName = [
        'default'         => 'wechat_reply_default',
        'follow'          => 'wechat_reply_follow',
        'customer_switch' => 'wechat_customer_switch',
	];

    protected $table = 'wechat_reply';

    //缓存过期时间
    protected $cacheTime = 86400*7;

    /**
     * 根据ID获取缓存图文消息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function cacheFindFromKeyword($wechat_keyword_id){
        $reply = Cache::get(config('app.name').':keyword:'.$wechat_keyword_id.':reply');
        if(!$reply){
            $reply = $this->where('wechat_keyword_id',$wechat_keyword_id)->first();
            if(!$reply){
            	return false;
            }
            Cache::put(config('app.name').':keyword:'.$wechat_keyword_id.':reply', $reply,$this->cacheTime);
            return $reply;
        }
        return $reply;
    }

    public function cacheEmpty($wechat_keyword_id){
        Cache::forget(config('app.name').':keyword:'.$wechat_keyword_id.':reply');
        return $this;
    }
}
